// pages/distribution/put-money.js
const format = require('../../utils/format.js')
const app = getApp()
const pool = new Map()
const tabs = [{
        name: "全部",
        status: 0
    },
    {
        name: "待审核",
        status: 1
    },
    {
        name: "已打款",
        status: 3
    },
    {
        name: "待打款",
        status: 2
    },
    {
        name: "未通过",
        status: -1
    },
]
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let statusObj = {}
        Object.keys(tabs).forEach(k => {
            statusObj[tabs[k].status] = tabs[k].name
        })
        this.setData({
            statusObj,
            tabs
        })
        this.loadData()
    },
    clickTab(evt) {
        const status = evt.currentTarget.dataset.status
        if (status != this.data.status) {
            this.setData({
                status
            })
            let tab = pool.get(this.data.status)
            if (tab && tab.list.length == 0) {
                tab.page = 1
                tab.total = 2,
                nextPage = true,
                page_num = 10
            }
            this.loadData()
        }
    },
    loadData() {
        let tab = pool.get(this.data.status)
        if (!tab) {
            tab = {
                page: 1,
                nextPage: true,
                list: [],
                page_num: 10
            }
            pool.set(this.data.status, tab)
        }
        this.setData(tab)
        if (!tab.nextPage) {
            this.setData(tab)
            return
        }

        if (tab.page == 1) {
            wx.showLoading({
                title: '加载中...',
            })
        }

        if (this.data.loading) {
            return
        }

        this.setData({
            loading: true
        })

        app.http.get("account/list", {
            data: {
                page: tab.page,
                page_num: tab.page_num,
                status: this.data.status,
                category_id: 2,
                type: 4,
            }
        }).then(res => {
            tab.list.push(...res.data.list.map(o => {
                let t = new Date(parseInt(o.created_at) * 1000)
                o.created_at = format.dateformat.format(t, 'yyyy-MM-dd hh:mm:ss')
                o.service_charge = (parseFloat(o.amount) - parseFloat(o.tally_amount)).toFixed(2)
                return o
            }))
            tab.page++
            tab.nextPage = res.data.next_page
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
            this.setData({
                loading: false
            })
        }).catch(err => {
            app.wxToast.error(err.message)
            this.setData({
                loading: false
            })
        })
    },
    onReachBottom() {
        this.loadData()
    }
})